package com.lmb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Category;
import com.lmb.bean.R;

import com.lmb.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: CategoryController
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/18 8:27
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R getList(Integer page , Integer pageSize){
        System.out.println(page + "  " + pageSize);
        Page<Category> categoryPage = new Page<>(page, pageSize);
        Page<Category> categoryPageList = categoryService.page(categoryPage);
        return R.success(categoryPageList);
    }


    @DeleteMapping
    public  R deleteByIdAssociated(Long ids){
        System.out.println(ids);
        categoryService.deleteByIdAssociated(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R upCategory(@RequestBody Category category, HttpSession session){
        System.out.println(category);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getName() !=null,Category::getName,category.getName());
        categoryLambdaQueryWrapper.ne(Category::getId,category.getId());
        int count = categoryService.count(categoryLambdaQueryWrapper);
        if (count>0){
            throw new ClassCastException("菜品名重复");
        }

        category.setUpdateUser((Long) session.getAttribute("employee"));
        category.setUpdateTime(LocalDateTime.now());


        categoryService.updateById(category);
        return R.success("修改成功！！！");
    }

    @PostMapping
    public R addCategoryl(@RequestBody Category category,HttpSession session){
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getName() !=null,Category::getName,category.getName());
        int count = categoryService.count(categoryLambdaQueryWrapper);
        if (count>0){
            throw new ClassCastException("已有该套餐或菜品");
        }

        Long employeeId = (Long) session.getAttribute("employee");
        category.setUpdateUser(employeeId);
        category.setCreateUser(employeeId);

        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        categoryService.save(category);


        return R.success("添加成功");
    }

    @GetMapping("/list")
    public R catgoeyListByType(Integer type){
        System.out.println(type);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(type!= null,Category::getType,type);
        List<Category> categoryList = categoryService.list(categoryLambdaQueryWrapper);
        return R.success(categoryList);
    }



}
